package com.qianfeng.userconsumer.controller;


//
//                            _ooOoo_  
//                           o8888888o  
//                           88" . "88  
//                           (| -_- |)  
//                            O\ = /O  
//                        ____/`---'\____  
//                      .   ' \\| |// `.  
//                       / \\||| : |||// \  
//                     / _||||| -:- |||||- \  
//                       | | \\\ - /// | |  
//                     | \_| ''\---/'' | |  
//                      \ .-\__ `-` ___/-. /  
//                   ___`. .' /--.--\ `. . __  
//                ."" '< `.___\_<|>_/___.' >'"".  
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |  
//                 \ \ `-. \_ __\ /__ _/ .-` / /  
//         ======`-.____`-.___\_____/___.-`____.-'======  
//                            `=---='  
//  
//         .............................................  
//                  佛祖镇楼            BUG辟易  
//          佛曰:  
//                  写字楼里写字间，写字间里程序员；  
//                  程序人员写程序，又拿程序换酒钱。  
//                  酒醒只在网上坐，酒醉还来网下眠；  
//                  酒醉酒醒日复日，网上网下年复年。  
//                  但愿老死电脑间，不愿鞠躬老板前；  
//                  奔驰宝马贵者趣，公交自行程序员。  
//                  别人笑我忒疯癫，我笑自己命太贱；  


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qianfeng.userconsumer.feign.AddressFeign;
import com.qianfeng.userconsumer.pojo.User;
import com.qianfeng.userconsumer.pojo.UserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

/**
 * Created by jackiechan on 2022/10/19 09:30
 *
 * @author jackiechan
 * @version 1.0
 * @since 1.0
 */

@RestController
@RequestMapping("/user")
@RefreshScope//当配置发生变化的时候会刷新
public class UserController {

    @Value("${yonghu.name}")
    private String name;
    private AddressFeign addressFeign;

    @Autowired
    public void setAddressFeign(AddressFeign addressFeign) {
        this.addressFeign = addressFeign;
    }

    @GetMapping("/info/{id}")
    public User findById(@PathVariable  Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("中国");
        //我们已经有注册中心了,应该从注册中心里面获取地址,这里的域名就换成了我们想要请求的程序在eureka中的名字
        UserAddress userAddress = addressFeign.findAddressById(id);
        user.setUserAddress(userAddress);
        return user;
    }

    @GetMapping("/info2/{id}")
    public User findById2(@PathVariable Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("花自飘零水自流,一种相思两处闲出,此情无计可消除,才下眉头却上心头");
        UserAddress userAddress = addressFeign.findByNameAndId(100, "山高");
        user.setUserAddress(userAddress);
        return user;
    }

    /**
     * fallbackMethod 降级方法, 降级指的是我们在遇到问题的时候提供的备选方案
     *
     * @return
     */
    @GetMapping("/test1")
    @HystrixCommand(fallbackMethod = "test1jiangji")
    public String test1() {
        System.err.println(Thread.currentThread().getName());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return "test1" + System.currentTimeMillis();
    }


    @GetMapping("/test2")
    public String test2(HttpServletRequest request) {
        System.err.println("UserController中的方法test2 执行了--->");
        return "test2v222222----" + System.currentTimeMillis();
    }


    @GetMapping("/test3")
    //commandProperties 修改一些属性
    @HystrixCommand(commandKey = "pangshaohuashuizhaole",fallbackMethod = "test3fallback",commandProperties ={@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")} )
    public String test3() {
        System.err.println(Thread.currentThread().getName());

        if (new Random().nextBoolean()) {
            int i = 1 / 0;
        }
        return "test3" + System.currentTimeMillis();
    }


    @GetMapping("/test4")
    //commandProperties 修改一些属性
    @HystrixCommand(fallbackMethod = "test3fallback",commandProperties ={@HystrixProperty(name = "execution.isolation.strategy",value = "SEMAPHORE")} )
    public String test4() {
        System.err.println("test4执行了");
        int i = 1 / 0;
        return "test3" + System.currentTimeMillis();
    }


    @GetMapping("/name")
    public String getName() {
        return name;
    }

    public String test1jiangji() {
        System.err.println(Thread.currentThread().getName());
        return "zheshitest1dejiangji";
    }


    public String test3fallback() {
        System.err.println(Thread.currentThread().getName());
        return "大学的恋情基本都不会有结局,所以早分晚分都得分";
    }
}
